#################################################
### Replication Code for Appendix A           ###
### Title: Mimicking Democracy                ###
### Authors: Hsu Yumin Wang, Eddy S. F. Yeung ###
### Version: April 26, 2024                   ###
#################################################

### Set-up ----
rm(list = ls())
setwd("~/Desktop/redist-propaganda/replication/appendix A")

library(haven)      # version 2.4.3
library(tidyverse)  # version 2.0.0
library(survey)     # version 1.1-1
library(cowplot)    # version 1.1.1
library(grid)       # version 4.0.1
library(gridExtra)  # version 2.3

### Import the dataset ----
df <- read_sav("GBS2.9.sav")
country_label <- read.csv("country_label_appendixA.csv")
df <- subset(df, wt > 0) # drop if weight is negative

### Subset the dataset (listwise deletion) ----
df <- subset(df, df$dem1 >= 1 & df$dem1 <= 4)
df <- subset(df, df$dem2 >= 1 & df$dem2 <= 4)
df <- subset(df, df$dem3 >= 1 & df$dem3 <= 4)
df <- subset(df, df$dem4 >= 1 & df$dem4 <= 4)

### Code the variables ----
df$soceq1 <- ifelse(df$dem1 == 1, 1, 0) # 1 = choosing social equity over others in Q.1
df$soceq2 <- ifelse(df$dem2 == 2, 1, 0) # 1 = choosing social equity over others in Q.2
df$soceq3 <- ifelse(df$dem3 == 3, 1, 0) # 1 = choosing social equity over others in Q.3
df$soceq4 <- ifelse(df$dem4 == 4, 1, 0) # 1 = choosing social equity over others in Q.4
df$soc_eq <- df$soceq1 + df$soceq2 + df$soceq3 + df$soceq4
df$soc_eq_bin <- ifelse(df$soc_eq == 0, 0, 1)

### Analysis: average number of social-equity statements chosen ----
## Asia
df_asia <- df %>% subset(region == 1 | region == 6)
df_wt_asia <- svydesign(ids = ~0, strata = NULL, weights = ~wt, data = df_asia)
df_sum_asia <- svyby(~soc_eq, ~country_un, df_wt_asia, svymean)
df_sum_asia <- country_label %>% 
  left_join(df_sum_asia, by = "country_un") %>% 
  drop_na()
df_sum_asia <- df_sum_asia[order(df_sum_asia$soc_eq), ]
df_sum_asia$country_label <- 
  factor(df_sum_asia$country_label,
         levels = c("KOR", "LKA", "MNG", "MYS", "IDN", "KHM", "JPN", "PHL",
                    "SGP", "IND", "BGD", "NPL", "HKG", "TWN", "PAK", "CHN", 
                    "THA", "VNM"))
p1 <-
  ggplot(df_sum_asia, aes(x = country_label, y = soc_eq)) +
  geom_bar(stat = "identity", position = "identity",
           color = "black", fill = "grey80") +
  coord_cartesian(ylim = c(0, 4)) +
  labs(y = "Social-Equity\nStatements Chosen", x = "") +
  ggtitle("Asia") +
  theme_bw() +
  theme(text = element_text(size = 12, family = "Times"),
        axis.text = element_text(color = "black"), 
        axis.title.y = element_text(size = 10),
        plot.title = element_text(size = 12, face = "italic"))

## Latin America
df_latin <- df %>% subset(region == 5)
df_wt_latin <- svydesign(ids = ~0, strata = NULL, weights = ~wt, data = df_latin)
df_sum_latin <- svyby(~soc_eq, ~country_un, df_wt_latin, svymean)
df_sum_latin <- country_label %>% 
  left_join(df_sum_latin, by = "country_un") %>% 
  drop_na()
df_sum_latin <- df_sum_latin[order(df_sum_latin$soc_eq), ]
df_sum_latin$country_label <- 
  factor(df_sum_latin$country_label,
         levels = c("CRI", "SLV", "BRA", "VEN", "PER", "NIC", "COL", "URY",
                    "PAN", "ARG", "BOL", "ESP", "CHL", "MEX", "PRY", "ECU",
                    "DOM"))
p2 <-
  ggplot(df_sum_latin, aes(x = country_label, y = soc_eq)) +
  geom_bar(stat = "identity", position = "identity",
           color = "black", fill = "grey80") +
  coord_cartesian(ylim = c(0, 4)) +
  labs(y = "Social-Equity\nStatements Chosen", x = "") +
  ggtitle("Latin America") +
  theme_bw() +
  theme(text = element_text(size = 12, family = "Times"),
        axis.text = element_text(color = "black"), 
        axis.title.y = element_text(size = 10),
        plot.title = element_text(size = 12, face = "italic"))

## Africa
df_africa <- df %>% subset(region == 2)
df_wt_africa <- svydesign(ids = ~0, strata = NULL, weights = ~wt, data = df_africa)
df_sum_africa <- svyby(~soc_eq, ~country_un, df_wt_africa, svymean)
df_sum_africa <- country_label %>% 
  left_join(df_sum_africa, by = "country_un") %>% 
  drop_na()
df_sum_africa <- df_sum_africa[order(df_sum_africa$soc_eq), ]
df_sum_africa$country_label <- 
  factor(df_sum_africa$country_label,
         levels = c("TZA", "CMR", "GHA", "SLE", "CIV", "UGA", "ZMB", "LBR",
                    "BDI", "MDG", "SDN", "TGO", "DZA", "BEN", "KEN", "CPV",
                    "GIN", "ZAF", "MUS", "NGA", "MWI", "LSO", "SEN", "ZWE",
                    "BFA", "NAM", "MOZ", "TUN", "SWZ", "EGY", "MLI", "MAR",
                    "NER"))
p3 <-
  ggplot(df_sum_africa, aes(x = country_label, y = soc_eq)) +
  geom_bar(stat = "identity", position = "identity",
           color = "black", fill = "grey80") +
  coord_cartesian(ylim = c(0, 4)) +
  labs(y = "Social-Equity\nStatements Chosen", x = "") +
  ggtitle("Africa") +
  theme_bw() +
  theme(text = element_text(size = 12, family = "Times"),
        axis.text.x = element_text(size = 8, color = "black"), 
        axis.title.y = element_text(size = 10),
        plot.title = element_text(size = 12, face = "italic"))

## Combine into one graph (Figure S1)
soceq_by_region <- plot_grid(p1, p2, p3, labels = "AUTO", ncol = 1, 
                             label_fontfamily = "Times")
ggsave(file = "soceq_by_region.pdf", soceq_by_region, width = 9, height = 6)

# Analysis: % of respondents choosing any social-equity statement ----
## Asia
df_sum_asia2 <- svyby(~soc_eq_bin, ~country_un, df_wt_asia, svymean)
df_sum_asia2 <- country_label %>% 
  left_join(df_sum_asia2, by = "country_un") %>% 
  drop_na()
df_sum_asia2 <- df_sum_asia2[order(df_sum_asia2$soc_eq_bin), ]
df_sum_asia2$country_label <- 
  factor(df_sum_asia2$country_label,
         levels = c("LKA", "KOR", "MNG", "MYS", "JPN", "IDN", "IND", "BGD", 
                    "NPL", "KHM", "SGP", "HKG", "TWN", "PHL", "THA", "CHN", 
                    "PAK", "VNM"))
p4 <-
  ggplot(df_sum_asia2, aes(x = country_label, y = soc_eq_bin * 100)) +
  geom_bar(stat = "identity", position = "identity",
           color = "black", fill = "grey80") +
  coord_cartesian(ylim = c(0, 100)) +
  labs(y = "At Least One\nStatement Chosen (%)", x = "") +
  ggtitle("Asia") +
  theme_bw() +
  theme(text = element_text(size = 12, family = "Times"),
        axis.text = element_text(color = "black"), 
        axis.title.y = element_text(size = 10),
        plot.title = element_text(size = 12, face = "italic"))

## Latin America
df_sum_latin2 <- svyby(~soc_eq_bin, ~country_un, df_wt_latin, svymean)
df_sum_latin2 <- country_label %>% 
  left_join(df_sum_latin2, by = "country_un") %>% 
  drop_na()
df_sum_latin2 <- df_sum_latin2[order(df_sum_latin2$soc_eq_bin), ]
df_sum_latin2$country_label <- 
  factor(df_sum_latin2$country_label,
         levels = c("BRA", "COL", "CRI", "VEN", "URY", "PER", "SLV", "NIC", 
                    "ARG", "CHL", "ESP", "PAN", "BOL", "DOM", "MEX", "ECU", 
                    "PRY"))
p5 <-
  ggplot(df_sum_latin2, aes(x = country_label, y = soc_eq_bin * 100)) +
  geom_bar(stat = "identity", position = "identity",
           color = "black", fill = "grey80") +
  coord_cartesian(ylim = c(0, 100)) +
  labs(y = "At Least One\nStatement Chosen (%)", x = "") +
  ggtitle("Latin America") +
  theme_bw() +
  theme(text = element_text(size = 12, family = "Times"),
        axis.text = element_text(color = "black"), 
        axis.title.y = element_text(size = 10),
        plot.title = element_text(size = 12, face = "italic"))

## Africa
df_sum_africa2 <- svyby(~soc_eq_bin, ~country_un, df_wt_africa, svymean)
df_sum_africa2 <- country_label %>% 
  left_join(df_sum_africa2, by = "country_un") %>% 
  drop_na()
df_sum_africa2 <- df_sum_africa2[order(df_sum_africa2$soc_eq_bin), ]
df_sum_africa2$country_label <- 
  factor(df_sum_africa2$country_label,
         levels = c("TZA", "CMR", "CIV", "GHA", "MDG", "ZMB", "UGA", "LBR", 
                    "DZA", "SLE", "TGO", "BDI", "BEN", "SDN", "CPV", "KEN", 
                    "ZWE", "SEN", "GIN", "BFA", "MUS", "LSO", "ZAF", "MOZ", 
                    "MWI", "NAM", "NGA", "SWZ", "TUN", "MLI", "MAR", "EGY", 
                    "NER"))
p6 <-
  ggplot(df_sum_africa2, aes(x = country_label, y = soc_eq_bin * 100)) +
  geom_bar(stat = "identity", position = "identity",
           color = "black", fill = "grey80") +
  coord_cartesian(ylim = c(0, 100)) +
  labs(y = "At Least One\nStatement Chosen (%)", x = "") +
  ggtitle("Africa") +
  theme_bw() +
  theme(text = element_text(size = 12, family = "Times"),
        axis.text.x = element_text(size = 8, color = "black"), 
        axis.title.y = element_text(size = 10),
        plot.title = element_text(size = 12, face = "italic"))

## Combine into one graph (Figure S2)
soceq_bin_by_region <- plot_grid(p4, p5, p6, labels = "AUTO", ncol = 1, 
                                 label_fontfamily = "Times")
ggsave(file = "soceq_bin_by_region.pdf", soceq_bin_by_region, width = 9, height = 6)
